package datalookin.network2;

import android.util.Log;

import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.Gson;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.VolleySingleton;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import inter.Delegate;

/**
 * Created by Administrator on 2016/6/5.
 */
public class GetWaterPlantRealTimeData  {

    private String waterPlantCode;
    private Delegate delegate;

    public void setWaterPlantCode(String waterPlantCode) {
        this.waterPlantCode = waterPlantCode;
    }

    public void setDelegate(Delegate delegate) {
        this.delegate = delegate;
    }

    public void get() {
        String ip = MyApplication.share().getResources().getString(R.string.IP_zj);
        String url = "http://"+ip+"/GetWaterPlantRealTimeData.action";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.i("实时厂区数据", response);
                if (delegate != null)
                    delegate.onResponse(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                if (delegate != null)
                    delegate.onErrorResponse(error);
            }
        }){

            @Override
            protected Map<String, String> getParams() {
                Map map = new HashMap();
                map.put("waterPlantCode", waterPlantCode);
                return map;
            }
            @Override
            protected Response<String> parseNetworkResponse(NetworkResponse response) {
                String temp = null;
                try {
                    temp = new String(response.data, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return Response.success(temp, HttpHeaderParser.parseCacheHeaders(response));
            }
        };
        VolleySingleton.getVolleySingleton(MyApplication.share()).addToRequestQueue(stringRequest);
    }

}
